Ext.define("Admin.view.decMakConsultation.NewConsultationController", {
    extend: "Ext.app.ViewController",
    alias: "controller.newConsultation",

    meetingChange: function(that, newValue, oldValue, eOpts) {
        var _that = this;
        var markGrid = _that.lookupReference("markGrid");
        Ext.Ajax.request({
            url: config.URL + "szy/dispatch/meeting/scompare/findByConId",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                _that.markStoreChange(result);
                _that.resultStoreChange(result);
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: { conId: newValue, river: config.RIVER }
        });
    },

    /**
     * 设置评分规则设置的表头和store
     */
    markStoreChange: function(data) {
        var _that = this;
        var rulesList = data.ruleslist;
        var factorlist = data.factorlist;
        var markGrid = _that.lookupReference("markGrid"); //评分规则表格
        var storeData = [],
            columns = [{ header: "类别", dataIndex: "name", align: "center", flex: 1 }];
        //生成store
        rulesList.forEach(function(item) {
            var list = {
                keys: item.keys,
                name: item.name
            };
            for (var key in item.map) {
                list[key] = item.map[key];
            }
            storeData.push(list);
        });
        //生成columns
        factorlist.forEach(function(item) {
            columns.push({
                header: item.factNm,
                dataIndex: item.factId,
                align: "center",
                flex: 1
            });
        });
        var store = Ext.create("Ext.data.Store", {
            fields: ["name", "F00001", "F00002", "F00003", "F00004"],
            data: storeData
        });
        markGrid.reconfigure(store);
    },

    /**
     * 更新评分规则表格
     * @param  {Object} editor   Ext.grid.plugin.CellEditing
     * @param  {Object} context An editing context with the following properties:
     * @param  {Object} eOpts   [description]
     * @return {Boolean}         返回是否退出修改状态
     */
    updateGrid: function(editor, context, eOpts) {
        var _that = this;
        var eventNm = _that.lookupReference("meetingName").getValue();
        console.log('context', context);
        var value = context.newValues;
        var rowIndex = context.rowIdx;
        var key = "",
            params = [];
        // params.conId = eventNm;
        if (
            rowIndex === 2 &&
            Number(value.F00001) +
            Number(value.F00002) +
            Number(value.F00003) +
            Number(value.F00004) !==
            1
        ) {
            Ext.Msg.alert("提示", "指标权重的和必须等于1！");
            return false;
        }
        switch (rowIndex) {
            case 0:
                key = "scoreMax";
                break;
            case 1:
                key = "scoreMin";
                break;
            case 2:
                key = "weight";
                break;
            default:
        }
        for (var i = 0; i < 4; i++) {
            params.push({ conId: eventNm, factId: 'F0000' + (i + 1) });
            params[i][key] = value['F0000' + (i + 1)];
        }
        var values = JSON.stringify(params);
        values.river = config.RIVER;
        Ext.Ajax.request({
            url: config.URL + "/szy/dispatch/meeting/DdsSFactorw/update",
            method: "post",
            defaultHeaders: { 'Content-Type': 'application/json' },
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 1) {
                    Ext.Msg.alert("系统提示", "修改成功");
                } else {
                    Ext.Msg.alert('系统提示', result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: values
        });
        return true;
    },

    saveScheme: function() {
        var _that = this;
        var eventNm = _that.lookupReference("meetingName").getValue();
        var curScheme = _that.lookupReference("resultGrid").curChecked;
        if (!curScheme) return;
        Ext.Ajax.request({
            url: config.URL + "/szy/dispatch/meeting/ddsSCon/updateProIdRec",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                console.log('saveScheme保存成功', result);
                if (result.code === 1) {
                    Ext.Msg.alert("系统提示", "保存成功");
                } else {
                    Ext.Msg.alert('系统提示', result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: { conId: eventNm, proIdRec: curScheme, river: config.RIVER }
        });
    },

    /**
     * 设置方案评价结果的store
     */
    resultStoreChange: function(data) {
        var _that = this;
        var edplist = data.edplist;
        var resultGrid = _that.lookupReference("resultGrid");
        var chart1 = _that.lookupReference("echart1").echarts;
        var chart2 = _that.lookupReference("echart2").echarts;
        var chart3 = _that.lookupReference("echart3").echarts;
        var chart4 = _that.lookupReference("echart4").echarts;
        var chart1Option = chart1.getOption();
        var chart2Option = chart2.getOption();
        var chart3Option = chart3.getOption();
        var chart4Option = chart4.getOption();
        var store = [],
            categoriesData = [],
            seriesData1 = [],
            seriesData2 = [],
            seriesData3 = [],
            seriesData4 = [];
        edplist.forEach(function(item) {
            var list = {
                keys: item.proCd,
                name: item.name,
                scores: item.scores,
                flag: item.flag
            };
            if (item.map) {
                for (var key in item.map) {
                    list[key] = item.map[key];
                }
            }
            store.push(list);
        });
        resultGrid.setStore({ data: store });
        store.forEach(function(item, index) {
            categoriesData.push((index + 1));
            if (item.F00001) {
                seriesData1.push(item.F00001);
            } else {
                seriesData1.push(0);
            }
            if (item.F00002) {
                seriesData2.push(item.F00002);
            } else {
                seriesData2.push(0);
            }
            if (item.F00003) {
                seriesData3.push(item.F00003);
            } else {
                seriesData3.push(0);
            }
            if (item.F00004) {
                seriesData4.push(item.F00004);
            } else {
                seriesData4.push(0);
            }
        });
        chart1Option.xAxis[0].data = categoriesData;
        chart1Option.series[0].data = seriesData1;
        chart1.setOption(chart1Option);
        chart1.resize();
        chart2Option.xAxis[0].data = categoriesData;
        chart2Option.series[0].data = seriesData2;
        chart2.setOption(chart2Option);
        chart2.resize();
        chart3Option.xAxis[0].data = categoriesData;
        chart3Option.series[0].data = seriesData3;
        chart3.setOption(chart3Option);
        chart3.resize();
        chart4Option.xAxis[0].data = categoriesData;
        chart4Option.series[0].data = seriesData4;
        chart4.setOption(chart4Option);
        chart4.resize();
    },
    expertUpdata: function(grid, rowIndex, colIndex) {
        var rec = grid.getStore().getAt(rowIndex);
        var _that = this;
        var win = this.lookupReference('warnUpdataWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '方案详情',
                width: 530,
                layout: 'fit',
                closeAction: 'hide',
                resizable: false,
                reference: 'warnUpdataWindow',
                items: {
                    xtype: 'consultationForm',
                    border: false,
                    reference: 'warnUpdataForm'
                }
            });
        }
        win.items.items[0].getForm().reset();
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsEdP/list",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText).list;
                console.log('store:aaaaaaaaaaaaaaa', result.list);
                var setdata = {};
                for (var i = 0; i < result.length; i++) {
                    if (rec.data.keys == result[i].proCd) {
                        setdata = result[i];
                        setdata.scores = rec.data.scores;
                        setdata.F00001 = rec.data.F00001;
                        setdata.F00002 = rec.data.F00002;
                        setdata.F00003 = rec.data.F00003;
                        setdata.F00004 = rec.data.F00004;
                        console.log(setdata);
                        win.items.items[0].getForm().setValues(setdata);
                    }
                }
            },
            params: { river: config.RIVER }
        });

        win.show();
    },

});